<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="采样点数" prop="SamplePoints">
            <el-col :span="22">
              <el-input v-model="form.SamplePoints" placeholder="请输入采样点数">
                <template slot="append">点</template>
              </el-input>
            </el-col>
<!--            <el-col :span="2" style="padding-left: 5px" >-->
<!--              <el-tooltip content="程序自动转为2的幂指数次值，如 1024、16384等" effect="light" placement="right">-->
<!--                <i class="el-icon-question"/>-->
<!--              </el-tooltip>-->
<!--            </el-col>-->
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="采样频率" prop="VibFrequency">
            <el-col :span="22">
              <!--              <el-input v-model="form.VibFrequency" placeholder="请输入采样频率">-->
              <!--                <template slot="append">Hz</template>-->
              <!--              </el-input>-->
              <el-select v-model="form.VibFrequency" placeholder="请选择采样频率">
                <el-option label="500Hz" key="500" value="500"/>
                <el-option label="1000Hz" key="1000" value="1000"/>
                <el-option label="2000Hz" key="2000" value="2000"/>
                <el-option label="4000Hz" key="4000" value="4000"/>
                <el-option label="8000Hz" key="8000" value="8000"/>
              </el-select>
            </el-col>
            <!--            <el-col :span="2" style="padding-left: 5px" >-->
            <!--              <el-tooltip content="500Hz-8000Hz" effect="light" placement="right">-->
            <!--                <i class="el-icon-question"/>-->
            <!--              </el-tooltip>-->
            <!--            </el-col>-->
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="加速度量程" prop="VibFullScale">
            <el-col :span="22">
              <el-select v-model="form.VibFullScale" placeholder="请选择加速度量程">
                <el-option label="2g" key="2" value="2"/>
                <el-option label="4g" key="4" value="4"/>
                <el-option label="8g" key="8" value="8"/>
                <el-option label="16g" key="16" value="16"/>
              </el-select>
            </el-col>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="采样模式" prop="SampleMode">
            <el-select v-model="form.SampleMode" placeholder="请选择采样模式">
<!--              <el-option label="边缘触发" key="0" value="0"/>-->
              <el-option label="定时采样" key="1" value="1"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="密集采集间隔" prop="RmsRunItv" v-if="form.SampleMode==='0'">
            <el-col :span="22">
              <el-input v-model="form.RmsRunItv" placeholder="请输入密集采集间隔">
                <template slot="append">秒</template>
              </el-input>
            </el-col>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="边缘RMS阈值" prop="RmsThreshold" v-if="form.SampleMode==='0'">
            <el-col :span="22">
              <el-input v-model="form.RmsThreshold" placeholder="请输入边缘端RMS阈值">
                <template slot="append">g</template>
              </el-input>
            </el-col>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="传输模式">
            <el-select v-model="form.TransferMode" placeholder="请选择传输模式">
              <el-option label="MQTT整包" key="0" value="0"/>
              <el-option label="HTTP整包" key="1" value="1"/>
              <el-option label="MQTT分包" key="2" value="2"/>
              <el-option label="HTTP分包" key="3" value="3"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="分包点数" prop="SubpackageSize" v-if="form.TransferMode>1">
            <el-select v-model="form.SubpackageSize" placeholder="请选择分包点数">
              <el-option label="100点" key="100" value="100"/>
              <el-option label="300点" key="300" value="300"/>
              <el-option label="500点" key="500" value="500"/>
              <el-option label="1000点" key="1000" value="1000"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="数据重传" prop="ReuploadFlag">
            <el-select v-model="form.ReuploadFlag" placeholder="请选择数据重传">
              <el-option label="是" key="1" value="1"/>
              <el-option label="否" key="0" value="0"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="启用配置" prop="StartUseFlag" v-show="false">
            <el-select v-model="form.StartUseFlag" placeholder="请选择是否启用配置">
              <el-option label="是" key="1" value="1"/>
              <el-option label="否" key="0" value="0"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="额定转速" prop="Rpm">
            <el-col :span="22">
              <el-input v-model="form.Rpm" placeholder="请输入额定转速">
                <template slot="append">转/分钟</template>
              </el-input>
            </el-col>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="bottom_wrap">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </div>
</template>
<script>
import {getEquipConfigByCode, updateEquipConfig} from "@/api/equip";
import {validatorNumber, validatorNumber2} from "@/utils/validate";

export  default {
  name: "SetEquip",
  props: ["equipmentCode"],
  data(){
    return {
      equip: {
        equipmentCode: "",
        config: ""
      },
      form: {},
      rules: {
        RmsRunItv: [
          {required: true, message: "密集采集间隔不能为空", trigger: "blur"},
          {validator: validatorNumber, message: "密集采集只能为正整数", trigger: "blur"}
        ],
        RmsThreshold: [
          {required: true, message: "边缘端RMS阈值不能为空", trigger: "blur"},
          {validator: validatorNumber2, message: "边缘端RMS阈值只能为数字且最多两位小数", trigger: ["blur", "change"]}
        ],
        Rpm: [
          {required: true, message: "额定转速不能为空", trigger: "blur"},
          {validator: validatorNumber, message: "额定转速只能为正整数", trigger: ["blur", "change"]}
        ]
      }
    }
  },
  watch: {
    equipmentCode() {
      this.$nextTick(() => {
        this.equip.equipmentCode = this.equipmentCode;
        this.reset();
        this.getEquipConfig();
      })
    },
  },
  created() {
    if (this.equipmentCode) {
      this.equip.equipmentCode = this.equipmentCode;
    }
    this.reset();
    this.getEquipConfig();
  },
  methods:{
    getEquipConfig() {
      getEquipConfigByCode(this.equip.equipmentCode).then(res => {
        if (res.code === 200 && res.data) {
          let config = JSON.parse(res.data.config);
          const value = config.iphm_sensor_point;
          const type = Object.prototype.toString.call(value);
          if (type === '[object String]') {
            this.form = JSON.parse(value);
          } else if (type === '[object Object]') {
            this.form = value;
          }
        }
      })
    },
    // 表单重置
    reset() {
      this.form = {
        SamplePoints: 18000,
        VibFrequency: "8000",
        VibFullScale: "16",
        ReuploadFlag: "1",
        StartUseFlag: "1",
        TransferMode: "0",
        SubpackageSize: "300",
        SampleMode: "1",
        RmsRunItv: 120,
        RmsThreshold: 0.1,
        Rpm: undefined
      };
      this.resetForm("form");
    },
    highestOneBit(number) {
      if (number < 0) {
        // 处理负数情况
        return 0x80000000; // 对于32位整数，最高有效位是第32位
      }
      if (number === 0) {
        return 0; // 对于0，最高有效位是0
      }

      let result = 1;
      while (result <= number) {
        result <<= 1;
      }
      return result >>> 1;
    },
    /** 关闭配置 */
    cancel() {
      this.$emit('cancel');
    },
    /** 配置提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // this.form.SamplePoints = this.highestOneBit(this.form.SamplePoints);
          let config = {
            iphm_sensor_point: this.form
          }
          console.log(config)
          this.equip.config = JSON.stringify(config)
          updateEquipConfig(this.equip).then(res => {
            if (res.code === 200) {
              this.$modal.msgSuccess("保存成功");
              this.cancel();
            }
          })
        }
      })
    }
  }
}
</script>
<style scoped>
.bottom_wrap {
  margin-top: 10px;
  text-align: right;
}
</style>